﻿'   CursorInfo.vb
'   WireBear Awesemulator
'
'   Created by Chris Kent
'   Copyright 2011 WireBear. All rights reserved
'
#Region "License Information"
'   WireBear Awesemulator is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   WireBear Awesemulator is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with WireBear Awesemulator - License.txt
'   If not, see <http://www.gnu.org/licenses/>.
#End Region


Imports System.Runtime.InteropServices

Public Class CursorInfo

    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1})", Name, Purpose)
    End Function

    Public Property Name() As String
    Public Property Purpose() As String
    Public Property ImagePath() As String

    Private _imagePathPress As String
    Public Property ImagePathPress() As String
        Get
            Return _imagePathPress
        End Get
        Set(ByVal value As String)
            _imagePathPress = value
            _detectPressedEvents = True
        End Set
    End Property

    Public Property HotSpot() As Point
    Public Property IsDummyCursor() As Boolean = False

    Private _detectPressedEvents As Boolean
    Public ReadOnly Property DetectPressedEvents() As Boolean
        Get
            Return _detectPressedEvents
        End Get
    End Property

#Region "Cursor Creation"

    <DllImport("user32.dll")> _
    Public Shared Function GetIconInfo(ByVal hIcon As IntPtr, ByRef pIconInfo As IconInfo) As Boolean
    End Function

    <DllImport("user32.dll")> _
    Public Shared Function CreateIconIndirect(ByRef icon As IconInfo) As IntPtr
    End Function

    Public Structure IconInfo
        Public fIcon As Boolean
        Public xHotspot As Integer
        Public yHotspot As Integer
        Public hbmMask As IntPtr
        Public hbmColor As IntPtr
    End Structure

    <DllImport("user32.dll")> _
    Private Shared Function SetSystemCursor(ByVal hCursor As IntPtr, ByVal id As Integer) As Boolean
    End Function

    <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
    Public Shared Function LoadCursor(ByVal hInstance As IntPtr, ByVal lpCursorName As Integer) As IntPtr
    End Function

    <DllImport("user32", CharSet:=CharSet.Auto)> _
    Public Shared Function SystemParametersInfo( _
            ByVal intAction As Integer, _
            ByVal intParam As Integer, _
            ByVal strParam As String, _
            ByVal intWinIniFlag As Integer) As Integer
        ' returns non-zero value if function succeeds
    End Function

    Private Const SPI_SETCURSORS As Integer = &H57

    Public Shared Function CreateCursor(ByVal Path As String, ByVal hotspot As Point) As Cursor
        Using Image As New Bitmap(Path)
            Return CreateCursor(Image, hotspot)
        End Using
    End Function

    Public Shared Function CreateCursor(ByVal img As Bitmap, ByVal hotspot As Point) As Cursor
        Dim ptr As IntPtr = img.GetHicon()
        Dim ii As New IconInfo
        GetIconInfo(ptr, ii)
        ii.xHotspot = hotspot.X
        ii.yHotspot = hotspot.Y
        ii.fIcon = False
        ptr = CreateIconIndirect(ii)
        Return New Cursor(ptr)
    End Function

    Public Shared Sub SetCursor(ByVal c As Cursor, ByVal t As OCR_SYSTEM_CURSORS)
        SetSystemCursor(c.Handle, t)
    End Sub

    Public Shared Sub RestoreCursors()
        SystemParametersInfo(SPI_SETCURSORS, 0, Nothing, 0)
    End Sub

    Public Enum OCR_SYSTEM_CURSORS As UInteger
        OCR_APPSTARTING = 32650
        OCR_NORMAL = 32512
        OCR_CROSS = 32515
        OCR_HAND = 32649
        OCR_HELP = 3265
        OCR_IBEAM = 32513
        OCR_NO = 32648
        OCR_SIZEALL = 32646
        OCR_SIZENESW = 32643
        OCR_SIZENS = 32645
        OCR_SIZENWSE = 32642
        OCR_SIZEWE = 32644
        OCR_UP = 32516
        OCR_WAIT = 32514
    End Enum

    Public Enum IDC_STANDARD_CURSORS As Integer
        IDC_ARROW = 32512
        IDC_IBEAM = 32513
        IDC_WAIT = 32514
        IDC_CROSS = 32515
        IDC_UPARROW = 32516
        IDC_SIZE = 32640
        IDC_ICON = 32641
        IDC_SIZENWSE = 32642
        IDC_SIZENESW = 32643
        IDC_SIZEWE = 32644
        IDC_SIZENS = 32645
        IDC_SIZEALL = 32646
        IDC_NO = 32648
        IDC_HAND = 32649
        IDC_APPSTARTING = 32650
        IDC_HELP = 32651
    End Enum
#End Region



End Class
